<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RocketMQ核心架构解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        :root {
            --primary: #2563eb;
            --primary-dark: #1d4ed8;
            --secondary: #7c3aed;
            --accent: #10b981;
            --text-primary: #1f2937;
            --text-secondary: #4b5563;
            --bg-light: #f9fafb;
            --bg-card: #ffffff;
        }
        
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: var(--text-primary);
            background-color: var(--bg-light);
            line-height: 1.6;
        }
        
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            color: var(--text-primary);
        }
        
        .hero {
            background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 100%);
            color: white;
        }
        
        .card {
            background: var(--bg-card);
            border-radius: 12px;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
            transition: transform 0.2s, box-shadow 0.2s;
        }
        
        .card:hover {
            transform: translateY(-4px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        
        .code-block {
            background-color: #1e293b;
            color: #e2e8f0;
            border-radius: 8px;
            font-family: 'Courier New', Courier, monospace;
        }
        
        .icon-box {
            width: 48px;
            height: 48px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 16px;
            flex-shrink: 0;
        }
        
        .feature-item {
            display: flex;
            padding: 16px;
            background: rgba(255, 255, 255, 0.8);
            border-radius: 12px;
            margin-bottom: 12px;
            transition: all 0.3s ease;
        }
        
        .feature-item:hover {
            background: white;
            transform: translateX(4px);
        }
        
        .nav-pill {
            transition: all 0.3s ease;
        }
        
        .nav-pill:hover {
            background-color: rgba(37, 99, 235, 0.1);
            color: var(--primary);
        }
        
        .nav-pill.active {
            background-color: var(--primary);
            color: white;
        }
        
        .diagram-container {
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero py-20 px-6 md:px-16 lg:px-24">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-12 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6">RocketMQ核心架构解析</h1>
                    <p class="text-xl opacity-90 mb-8">深入剖析分布式消息队列的设计哲学与实现原理</p>
                    <div class="flex flex-wrap gap-4">
                        <a href="#nameserver" class="bg-white text-blue-700 px-6 py-3 rounded-lg font-medium hover:bg-opacity-90 transition">
                            <i class="fas fa-server mr-2"></i>NameServer解析
                        </a>
                        <a href="#broker" class="bg-blue-700 bg-opacity-20 text-white px-6 py-3 rounded-lg font-medium hover:bg-opacity-30 transition">
                            <i class="fas fa-database mr-2"></i>Broker架构
                        </a>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="diagram-container">
                        <div class="mermaid">
                            graph TD
                                A[Producer] -->|发送消息| B[Broker]
                                B -->|注册路由| C[NameServer]
                                D[Consumer] -->|获取路由| C
                                D -->|消费消息| B
                                B1[Broker Master] --> B2[Broker Slave]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-6xl px-6 py-12">
        <div class="flex flex-col lg:flex-row gap-8">
            <!-- Navigation -->
            <div class="lg:w-1/4">
                <div class="card p-6 sticky top-6">
                    <h3 class="text-xl font-bold mb-4 text-blue-700 flex items-center">
                        <i class="fas fa-bookmark mr-2"></i>内容导航
                    </h3>
                    <ul class="space-y-2">
                        <li>
                            <a href="#nameserver" class="nav-pill block px-4 py-2 rounded-lg font-medium flex items-center">
                                <i class="fas fa-server mr-3 text-blue-500"></i>
                                NameServer工作原理
                            </a>
                        </li>
                        <li>
                            <a href="#broker" class="nav-pill block px-4 py-2 rounded-lg font-medium flex items-center">
                                <i class="fas fa-database mr-3 text-purple-500"></i>
                                Broker架构设计
                            </a>
                        </li>
                        <li>
                            <a href="#producer" class="nav-pill block px-4 py-2 rounded-lg font-medium flex items-center">
                                <i class="fas fa-paper-plane mr-3 text-green-500"></i>
                                Producer机制
                            </a>
                        </li>
                    </ul>
                </div>
            </div>

            <!-- Content -->
            <div class="lg:w-3/4">
                <!-- NameServer Section -->
                <section id="nameserver" class="mb-20">
                    <div class="flex items-center mb-8">
                        <div class="icon-box bg-blue-100 text-blue-700">
                            <i class="fas fa-server text-xl"></i>
                        </div>
                        <h2 class="text-3xl font-bold">NameServer工作原理</h2>
                    </div>
                    
                    <div class="card p-8 mb-8">
                        <h3 class="text-2xl font-semibold mb-6 text-blue-700">架构设计与功能职责</h3>
                        <p class="text-gray-700 mb-6">NameServer是RocketMQ的核心组件之一，它充当着服务发现和路由的角色。在RocketMQ的架构中，NameServer提供轻量级的服务注册与发现功能，允许生产者和消费者找到相应的Broker。</p>
                        
                        <div class="grid md:grid-cols-2 gap-6 mb-8">
                            <div>
                                <h4 class="text-lg font-semibold mb-4 text-gray-800">核心功能</h4>
                                <ul class="space-y-3">
                                    <li class="feature-item">
                                        <i class="fas fa-route text-blue-500 mr-3"></i>
                                        <div>
                                            <h5 class="font-medium">路由注册</h5>
                                            <p class="text-sm text-gray-600">Broker启动后向所有NameServer注册，提供Topic路由信息</p>
                                        </div>
                                    </li>
                                    <li class="feature-item">
                                        <i class="fas fa-search text-blue-500 mr-3"></i>
                                        <div>
                                            <h5 class="font-medium">路由发现</h5>
                                            <p class="text-sm text-gray-600">生产者和消费者通过NameServer获取Topic的路由信息</p>
                                        </div>
                                    </li>
                                    <li class="feature-item">
                                        <i class="fas fa-trash-alt text-blue-500 mr-3"></i>
                                        <div>
                                            <h5 class="font-medium">路由删除</h5>
                                            <p class="text-sm text-gray-600">检测Broker存活状态，移除不可用的Broker</p>
                                        </div>
                                    </li>
                                    <li class="feature-item">
                                        <i class="fas fa-cube text-blue-500 mr-3"></i>
                                        <div>
                                            <h5 class="font-medium">无状态设计</h5>
                                            <p class="text-sm text-gray-600">每个NameServer节点独立工作，不相互通信</p>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                            <div>
                                <h4 class="text-lg font-semibold mb-4 text-gray-800">架构特点</h4>
                                <ul class="space-y-3">
                                    <li class="feature-item">
                                        <i class="fas fa-bolt text-blue-500 mr-3"></i>
                                        <div>
                                            <h5 class="font-medium">简单高效</h5>
                                            <p class="text-sm text-gray-600">NameServer采用了极简设计，没有复杂的状态同步机制</p>
                                        </div>
                                    </li>
                                    <li class="feature-item">
                                        <i class="fas fa-memory text-blue-500 mr-3"></i>
                                        <div>
                                            <h5 class="font-medium">内存存储</h5>
                                            <p class="text-sm text-gray-600">所有路由信息存储在内存中，提供快速的读写操作</p>
                                        </div>
                                    </li>
                                    <li class="feature-item">
                                        <i class="fas fa-clock text-blue-500 mr-3"></i>
                                        <div>
                                            <h5 class="font-medium">定时扫描</h5>
                                            <p class="text-sm text-gray-600">每10秒扫描一次Broker，移除不活跃的Broker</p>
                                        </div>
                                    </li>
                                    <li class="feature-item">
                                        <i class="fas fa-expand text-blue-500 mr-3"></i>
                                        <div>
                                            <h5 class="font-medium">水平扩展</h5>
                                            <p class="text-sm text-gray-600">可部署多个NameServer节点，提高可用性</p>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                        </div>

                        <div class="mb-8">
                            <h4 class="text-lg font-semibold mb-4 text-gray-800">路由注册与发现机制</h4>
                            <div class="diagram-container">
                                <div class="mermaid">
                                    sequenceDiagram
                                        participant Broker
                                        participant NameServer
                                        participant Producer
                                        Broker->>NameServer: 启动注册 (RegisterBroker)
                                        Broker->>NameServer: 定时心跳 (每30秒)
                                        Producer->>NameServer: 获取路由 (GetRouteInfo)
                                        NameServer-->>Producer: 返回路由信息
                                        Producer->>Broker: 发送消息
                                </div>
                            </div>
                        </div>

                        <div class="mb-8">
                            <h4 class="text-lg font-semibold mb-4 text-gray-800">路由注册核心源码</h4>
                            <div class="code-block p-4 overflow-x-auto">
                                <pre><code class="language-java">
// org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager
public RegisterBrokerResult registerBroker(...) {
    // 锁定对路由表的操作
    try {
        // 更新集群信息
        this.clusterAddrTable.put(clusterName, brokerNames);
        
        // 更新Broker信息
        BrokerData brokerData = this.brokerAddrTable.get(brokerName);
        if (brokerData == null) {
            brokerData = new BrokerData(clusterName, brokerName, new HashMap<>());
            this.brokerAddrTable.put(brokerName, brokerData);
        }
        brokerData.getBrokerAddrs().put(brokerId, brokerAddr);
        
        // 更新Topic路由信息
        if (topicConfigWrapper != null && topicConfigWrapper.getTopicConfigTable() != null) {
            for (TopicConfig topicConfig : topicConfigWrapper.getTopicConfigTable().values()) {
                createAndUpdateQueueData(brokerName, topicConfig);
            }
        }
        
        // 更新Broker存活信息
        BrokerLiveInfo prevBrokerLiveInfo = this.brokerLiveTable.put(
            brokerAddr,
            new BrokerLiveInfo(
                System.currentTimeMillis(),
                topicConfigWrapper.getDataVersion(),
                channel,
                haServerAddr));
    } finally {
        // 释放锁
    }
}
                                </code></pre>
                            </div>
                        </div>
                    </div>
                </section>

                <!-- Broker Section -->
                <section id="broker" class="mb-20">
                    <div class="flex items-center mb-8">
                        <div class="icon-box bg-purple-100 text-purple-700">
                            <i class="fas fa-database text-xl"></i>
                        </div>
                        <h2 class="text-3xl font-bold">Broker架构设计</h2>
                    </div>
                    
                    <div class="card p-8 mb-8">
                        <h3 class="text-2xl font-semibold mb-6 text-purple-700">角色与工作模式</h3>
                        
                        <div class="grid md:grid-cols-2 gap-8 mb-8">
                            <div>
                                <h4 class="text-lg font-semibold mb-4 text-gray-800">Broker角色</h4>
                                <div class="bg-gray-50 p-4 rounded-lg">
                                    <ul class="space-y-3">
                                        <li class="flex items-start">
                                            <i class="fas fa-crown text-yellow-500 mt-1 mr-2"></i>
                                            <div>
                                                <span class="font-medium">Master</span>
                                                <p class="text-sm text-gray-600">主服务器，负责读写操作，默认Broker ID为0</p>
                                            </div>
                                        </li>
                                        <li class="flex items-start">
                                            <i class="fas fa-clone text-blue-500 mt-1 mr-2"></i>
                                            <div>
                                                <span class="font-medium">Slave</span>
                                                <p class="text-sm text-gray-600">从服务器，负责数据冗余，Broker ID大于0</p>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <div>
                                <h4 class="text-lg font-semibold mb-4 text-gray-800">工作模式</h4>
                                <div class="bg-gray-50 p-4 rounded-lg">
                                    <ul class="space-y-3">
                                        <li class="flex items-start">
                                            <i class="fas fa-server text-gray-500 mt-1 mr-2"></i>
                                            <div>
                                                <span class="font-medium">单Master模式</span>
                                                <p class="text-sm text-gray-600">简单，无需复制，存在单点风险</p>
                                            </div>
                                        </li>
                                        <li class="flex items-start">
                                            <i class="fas fa-sitemap text-blue-500 mt-1 mr-2"></i>
                                            <div>
                                                <span class="font-medium">多Master模式</span>
                                                <p class="text-sm text-gray-600">无单点风险，所有Master独立工作</p>
                                            </div>
                                        </li>
                                        <li class="flex items-start">
                                            <i class="fas fa-exchange-alt text-purple-500 mt-1 mr-2"></i>
                                            <div>
                                                <span class="font-medium">多Master多Slave(异步)</span>
                                                <p class="text-sm text-gray-600">性能好，可能丢失少量消息</p>
                                            </div>
                                        </li>
                                        <li class="flex items-start">
                                            <i class="fas fa-sync-alt text-green-500 mt-1 mr-2"></i>
                                            <div>
                                                <span class="font-medium">多Master多Slave(同步)</span>
                                                <p class="text-sm text-gray-600">数据不丢失，性能相对较低</p>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </div>

                        <div class="mb-8">
                            <h4 class="text-lg font-semibold mb-4 text-gray-800">存储架构设计</h4>
                            <div class="grid md:grid-cols-3 gap-4 mb-6">
                                <div class="bg-gray-50 p-4 rounded-lg">
                                    <h5 class="font-medium flex items-center mb-2">
                                        <i class="fas fa-file-alt text-blue-500 mr-2"></i>
                                        CommitLog
                                    </h5>
                                    <p class="text-sm text-gray-600">消息主体以及元数据的存储主体，单个文件大小默认1G</p>
                                </div>
                                <div class="bg-gray-50 p-4 rounded-lg">
                                    <h5 class="font-medium flex items-center mb-2">
                                        <i class="fas fa-list-ul text-purple-500 mr-2"></i>
                                        ConsumeQueue
                                    </h5>
                                    <p class="text-sm text-gray-600">消息消费队列，引用了CommitLog中的消息</p>
                                </div>
                                <div class="bg-gray-50 p-4 rounded-lg">
                                    <h5 class="font-medium flex items-center mb-2">
                                        <i class="fas fa-search text-green-500 mr-2"></i>
                                        IndexFile
                                    </h5>
                                    <p class="text-sm text-gray-600">消息索引文件，提供根据key或时间区间查询消息的能力</p>
                                </div>
                            </div>
                            <div class="diagram-container">
                                <div class="mermaid">
                                    graph LR
                                        A[Producer] --> B[CommitLog]
                                        B --> C[ConsumeQueue]
                                        C --> D[Consumer]
                                        B --> E[IndexFile]
                                        E --> F[Message Query]
                                </div>
                            </div>
                        </div>

                        <div class="mb-8">
                            <h4 class="text-lg font-semibold mb-4 text-gray-800">Broker启动流程源码</h4>
                            <div class="code-block p-4 overflow-x-auto">
                                <pre><code class="language-java">
// org.apache.rocketmq.broker.BrokerController
public boolean initialize() {
    // 加载配置
    this.topicConfigManager.load();
    this.consumerOffsetManager.load();
    this.subscriptionGroupManager.load();
    this.consumerFilterManager.load();
    
    // 创建消息存储服务
    this.messageStore = new DefaultMessageStore(
        this.messageStoreConfig,
        this.brokerStatsManager,
        this.messageArrivingListener,
        this.brokerConfig);
    
    // 初始化消息存储服务
    boolean result = this.messageStore.load();
    if (!result) {
        return false;
    }
    
    // 创建网络服务器
    this.remotingServer = new NettyRemotingServer(this.nettyServerConfig, this.clientHousekeepingHandler);
    this.fastRemotingServer = new NettyRemotingServer(this.nettyServerConfig, this.clientHousekeepingHandler);
    
    // 注册处理器
    this.registerProcessor();
    
    // 启动各种定时任务
    this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            BrokerController.this.consumerOffsetManager.persist();
        }
    }, 1000 * 10, this.brokerConfig.getFlushConsumerOffsetInterval(), TimeUnit.MILLISECONDS);
    
    // 向NameServer注册
    this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            BrokerController.this.registerBrokerAll(true, false, brokerConfig.isForceRegister());
        }
    }, 1000 * 10, Math.max(10000, this.brokerConfig.getRegisterNameServerPeriod()), TimeUnit.MILLISECONDS);
    
    return true;
}
                                </code></pre>
                            </div>
                        </div>
                    </div>
                </section>

                <!-- Producer Section -->
                <section id="producer">
                    <div class="flex items-center mb-8">
                        <div class="icon-box bg-green-100 text-green-700">
                            <i class="fas fa-paper-plane text-xl"></i>
                        </div>
                        <h2 class="text-3xl font-bold">Producer内部实现机制</h2>
                    </div>
                    
                    <div class="card p-8">
                        <h3 class="text-2xl font-semibold mb-6 text-green-700">消息发送流程与状态管理</h3>
                        
                        <div class="mb-8">
                            <h4 class="text-lg font-semibold mb-4 text-gray-800">发送流程</h4>
                            <div class="grid md:grid-cols-3 gap-4 mb-6">
                                <div class="feature-item">
                                    <i class="fas fa-plus-circle text-blue-500 mr-3"></i>
                                    <div>
                                        <h5 class="font-medium">消息创建</h5>
                                        <p class="text-sm text-gray-600">创建Message对象，设置Topic、Tag、Key和消息体</p>
                                    </div>
                                </div>
                                <div class="feature-item">
                                    <i class="fas fa-map-marked-alt text-purple-500 mr-3"></i>
                                    <div>
                                        <h5 class="font-medium">路由获取</h5>
                                        <p class="text-sm text-gray-600">从NameServer获取Topic的路由信息</p>
                                    </div>
                                </div>
                                <div class="feature-item">
                                    <i class="fas fa-random text-green-500 mr-3"></i>
                                    <div>
                                        <h5 class="font-medium">队列选择</h5>
                                        <p class="text-sm text-gray-600">根据负载均衡策略选择一个消息队列</p>
                                    </div>
                                </div>
                                <div class="feature-item">
                                    <i class="fas fa-paper-plane text-red-500 mr-3"></i>
                                    <div>
                                        <h5 class="font-medium">消息发送</h5>
                                        <p class="text-sm text-gray-600">将消息发送到选定的Broker</p>
                                    </div>
                                </div>
                                <div class="feature-item">
                                    <i class="fas fa-check-circle text-yellow-500 mr-3"></i>
                                    <div>
                                        <h5 class="font-medium">结果处理</h5>
                                        <p class="text-sm text-gray-600">处理发送结果，包括成功、失败或超时</p>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="mb-8">
                            <h4 class="text-lg font-semibold mb-4 text-gray-800">发送方式对比</h4>
                            <div class="overflow-x-auto">
                                <table class="min-w-full bg-white rounded-lg overflow-hidden">
                                    <thead class="bg-gray-100">
                                        <tr>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">发送方式</th>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">可靠性</th>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">性能</th>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">适用场景</th>
                                        </tr>
                                    </thead>
                                    <tbody class="divide-y divide-gray-200">
                                        <tr>
                                            <td class="px-6 py-4 whitespace-nowrap font-medium">同步发送</td>
                                            <td class="px-6 py-4 whitespace-nowrap text-green-600">高</td>
                                            <td class="px-6 py-4 whitespace-nowrap text-yellow-600">中</td>
                                            <td class="px-6 py-4">重要通知、短信通知等</td>
                                        </tr>
                                        <tr>
                                            <td class="px-6 py-4 whitespace-nowrap font-medium">异步发送</td>
                                            <td class="px-6 py-4 whitespace-nowrap text-yellow-600">中</td>
                                            <td class="px-6 py-4 whitespace-nowrap text-green-600">高</td>
                                            <td class="px-6 py-4">对响应时间敏感的业务场景</td>
                                        </tr>
                                        <tr>
                                            <td class="px-6 py-4 whitespace-nowrap font-medium">单向发送</td>
                                            <td class="px-6 py-4 whitespace-nowrap text-red-600">低</td>
                                            <td class="px-6 py-4 whitespace-nowrap text-green-600">最高</td>
                                            <td class="px-6 py-4">日志收集等允许消息丢失的场景</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <div class="mb-8">
                            <h4 class="text-lg font-semibold mb-4 text-gray-800">路由获取核心源码</h4>
                            <div class="code-block p-4 overflow-x-auto">
                                <pre><code class="language-java">
// org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl
private TopicPublishInfo tryToFindTopicPublishInfo(final String topic) {
    // 从缓存中获取路由信息
    TopicPublishInfo topicPublishInfo = this.topicPublishInfoTable.get(topic);
    if (null == topicPublishInfo || !topicPublishInfo.ok()) {
        this.topicPublishInfoTable.putIfAbsent(topic, new TopicPublishInfo());
        // 从NameServer更新路由信息
        this.mQClientFactory.updateTopicRouteInfoFromNameServer(topic);
        topicPublishInfo = this.topicPublishInfoTable.get(topic);
    }
    
    if (topicPublishInfo.isHaveTopicRouterInfo() || topicPublishInfo.ok()) {
        return topicPublishInfo;
    } else {
        // 尝试使用默认Topic获取路由信息
        this.mQClientFactory.updateTopicRouteInfoFromNameServer(topic, true, this.defaultMQProducer);
        topicPublishInfo = this.topicPublishInfoTable.get(topic);
        return topicPublishInfo;
    }
}
                                </code></pre>
                            </div>
                        </div>
                    </div>
                </section>
            </div>
        </div>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            sequence: {
                diagramMarginX: 50,
                diagramMarginY: 10,
                boxTextMargin: 5,
                noteMargin: 10,
                mirrorActors: true
            }
        });
    </script>
</body>
</html>